from django.shortcuts import render

# Create your views here.
from django.shortcuts import render
from rest_framework.viewsets import ReadOnlyModelViewSet

from .models import Area
from .serializers import AreaSerializer, SubAreaSerializer
from rest_framework_extensions.cache.mixins import CacheResponseMixin


# Create your views here.


class AreasViewSet(ReadOnlyModelViewSet, CacheResponseMixin):
    """
    行政区划信息
    """""
    pagination_class = None  # 区划信息不分页

    def get_queryset(self):
        """
        提供数据集(指定查询集)
        """""
        # 执行查询集
        # queryset = Area.objects.all()
        if self.action == 'list':
            # 　查询省级数据
            return Area.objects.filter(parent=None)
        else:
            # 指定的是retrieve行为,自带pk
            # 　查询城市和区县的数据
            return Area.objects.all()

    def get_serializer_class(self):
        """
        提供序列化器(指定序列化器)
        """""
        if self.action == 'list':
            return AreaSerializer
        else:
            return SubAreaSerializer
